window.addEventListener('load', function () {
    var box = document.querySelector('.box')
    var ul = document.querySelector('ul');
    var ol = document.querySelector('ol');
    var btnleft = document.querySelector('.arrowl');
    var btnright = document.querySelector('.arrowr');
    var Width = ul.children[0].offsetWidth//图片移动的宽度
    var num = 0  //记录点击次数
    var circle = 0 //记录小圆点
    var flag = true //节流阀
    //添加鼠标经过事件
    box.addEventListener('mouseenter', function () {
        btnright.style.display = 'block'
        btnleft.style.display = 'block'
        clearInterval(timer)
        timer = null
    })
    //添加鼠标离开事件
    box.addEventListener('mouseleave', function () {
        btnright.style.display = 'none'
        btnleft.style.display = 'none'
        timer = setInterval(function () {
            //手动调用点击事件
            btnright.click();
        }, 2000)
    })
    //动态生成小圆点
    for (var i = 0; i < ul.children.length; i++) {
        var li = document.createElement('li');
        li.setAttribute('index', i)
        ol.append(li);
        //生成小圆圈时添加绑定事件
        li.addEventListener('click', function () {
            //排他
            for (var i = 0; i < ol.children.length; i++) {
                ol.children[i].className = ''
            }
            this.className = 'current'
            //点击小圆圈，移动图片，移动ul
            var index = this.getAttribute('index')
            num = circle = index
            animate(ul, -index * Width)
        })
    }
    //把ol里面第一个小li设置成current
    ol.children[0].className = 'current'
    //克隆第一张图片
    var first = ul.children[0].cloneNode(true)
    ul.append(first)
    //为右侧按钮添加移动事件
    btnright.addEventListener('click', function () {
        if (flag) {
            flag = false;
            //animate(obj,target,callback)
            if (num == ul.children.length - 1) {
                ul.style.left = 0
                num = 0
            }
            num++
            circle++
            if (circle == ol.children.length) {
                circle = 0
            }
            for (var i = 0; i < ol.children.length; i++) {
                ol.children[i].className = ''
            }
            ol.children[circle].className = 'current'
            animate(ul, -num * Width, function () {
                flag = true
            })
        }
    })
    //为左侧按钮添加点击事件
    btnleft.addEventListener('click', function () {
        if (flag) {
            flag = false;
            //animate(obj,target,callback)
            if (num == 0) {
                num = ul.children.length - 1
                ul.style.left = -num * Width + 'px'
            }
            num--
            circle--
            if (circle < 0) {
                circle = ol.children.length - 1
            }
            for (var i = 0; i < ol.children.length; i++) {
                ol.children[i].className = ''
            }
            ol.children[circle].className = 'current'
            animate(ul, -num * Width,function(){
                flag=true
            })
        }
    })
    //自动播放功能
    var timer = setInterval(function () {
        //手动调用点击事件
        btnright.click();
    }, 2000)

})